package conMgr.ui.papers;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

import conMgr.model.Conference;
import conMgr.model.Paper;
import conMgr.model.User;
import conMgr.ui.util.Ui;

/**
 * Provides a subview consisting of just the paper info for the PaperView. 
 * @author is4@uw.edu
 */
public class PaperInfoView extends JPanel
{
	/**
	 * The serialization ID for this class.
	 */
	private static final long serialVersionUID = 705588401296331226L;

	/**
	 * The paper abstract text box.
	 */
	private final JTextComponent abstractTextBox;
	
	/**
	 * The category text box.
	 */
	private final JTextComponent categoryTextBox;
	
	/**
	 * The conference to submit a new paper to. This may be null if the context
	 * is editing an existing paper.
	 */
	private final Conference conference;
	
	/**
	 * The keywords text box.
	 */
	private final JTextComponent keywordsTextBox;
	
	/**
	 * The paper being edited. This may be null if the context is submitting a
	 * new paper.
	 */
	private final Paper paper;
	
	/**
	 * The title text box.
	 */
	private final JTextComponent titleTextBox;
	
	/**
	 * The document text box.
	 */
	private final JTextComponent docTextBox;
	
	/**
	 * Initializes a new paper info view.
	 * @param paper The paper being edited. This may be null if the context is
	 * submitting a new paper. 
	 * @param conference The conference to submit a new paper to. This may be
	 * null is the context is editing and existing paper.
	 */
	public PaperInfoView(final Paper paper, final Conference conference)
	{
		this.paper = paper;
		this.conference = conference;
		
		final User author =
			paper != null ? paper.getAuthor() : User.getCurrentUser();
		
		setLayout(new GridBagLayout());
		
		int row = 0;
		addGridBagFieldLabel("Conference:", row++);
		if (User.getCurrentUser().getType().canSeePaperSubprogramChair())
			addGridBagFieldLabel("Subprogram Chair:", row++);
		addGridBagFieldLabel("Author:", row++);
		addGridBagFieldLabel("Title:", row++);
		addGridBagFieldLabel("Abstract:", row++);
		addGridBagFieldLabel("Document Text:", row++);
		addGridBagFieldLabel("Keywords:", row++);
		addGridBagFieldLabel("Category:", row++);
		
		row = 0;
		addGridBagValueLabel(conference.getName(), row++);
		if (User.getCurrentUser().getType().canSeePaperSubprogramChair())
			addGridBagValueLabel((paper != null && paper.getSubprogramChair() != null ? paper.getSubprogramChair().getName() : "(unassigned)"), row++);
		addGridBagValueLabel(author.getName(), row++);
		titleTextBox = addGridBagValueTextBox(row++, (paper != null ? paper.getTitle() : ""), false);
		abstractTextBox = addGridBagValueTextBox(row++, (paper != null ? paper.getPaperAbstract() : ""),  true);
		docTextBox = addGridBagValueTextBox(row++, (paper != null ? paper.getDocText() : ""), true);
		keywordsTextBox = addGridBagValueTextBox(row++, (paper != null ? paper.getKeywords() : ""), false);
		categoryTextBox = addGridBagValueTextBox(row++, (paper != null ? paper.getCategories() : ""), false);
	}
	
	/**
	 * Adds a new field label to a grid bag.
	 * @param text The text to display in the label.
	 * @param row The row of the grid that the label belongs in.
	 */
	private void addGridBagFieldLabel(final String text, final int row)
	{
		final JLabel label = new JLabel(text);
		final GridBagConstraints constraints = new GridBagConstraints();
		constraints.gridx = 0;
		constraints.gridy = row;
		constraints.anchor = GridBagConstraints.NORTHWEST;
		constraints.insets = Ui.DEFAULT_INSETS;
		add(label, constraints);
	}
	
	/**
	 * Adds a new value label to a grid bag.
	 * @param text The text to display in the label.
	 * @param row The row of the grid that the label belongs in.
	 */
	private void addGridBagValueLabel(final String text, final int row)
	{
		final JLabel label = new JLabel(text);
		label.setFont(Ui.getUnboldFont(label.getFont()));
		
		final GridBagConstraints constraints = new GridBagConstraints();
		constraints.gridx = 1;
		constraints.gridy = row;
		constraints.anchor = GridBagConstraints.NORTHWEST;
		constraints.insets = Ui.DEFAULT_INSETS;
		add(label, constraints);
	}
	
	/**
	 * Adds a new empty text box to a grid bag.
	 * @param row The row of the grid that the text box belongs in.
	 * @param text The text to put in the text box.
	 * @return The newly-created text box.
	 */
	private JTextComponent addGridBagValueTextBox
	(
		final int row,
		final String text,
		boolean large
	)
	{
		JComponent addControl;
		JTextComponent field;
		
		if (large)
		{
			final JTextArea textArea = new JTextArea(5, 30);
			textArea.setLineWrap(true);
			textArea.setWrapStyleWord(true);
			field = textArea;
			
			addControl = new JScrollPane(textArea);
		}
		else
			addControl = field = new JTextField(30);
		
		if (paper != null)
		{
			field.setText(text);
			field.setEditable(false);
			field.setBackground(Color.WHITE);
		}
		
		final GridBagConstraints constraints = new GridBagConstraints();
		constraints.gridx = 1;
		constraints.gridy = row;
		constraints.anchor = GridBagConstraints.NORTHWEST;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.insets = Ui.DEFAULT_INSETS;
		add(addControl, constraints);
		
		return field;
	}
	
	/**
	 * Creates a new paper with the information entered in the text boxes.
	 * @return The newly created paper or null if errors were encountered.
	 */
	public Paper createPaper()
	{
		final String title = titleTextBox.getText();
		final String paperAbstract = abstractTextBox.getText();
		final String keywords = keywordsTextBox.getText();
		final String categories = categoryTextBox.getText();
		final String docText = docTextBox.getText();
		
		if
		(
			Ui.isBlank(title) ||
			Ui.isBlank(paperAbstract) ||
			Ui.isBlank(docText) ||
			Ui.isBlank(keywords) ||
			Ui.isBlank(categories)
		)
		{
			JOptionPane.showMessageDialog
			(
				null,
				"Please fill out all of the fields. A paper cannot be created with missing information.",
				"Cannot Submit Paper",
				JOptionPane.ERROR_MESSAGE
			);
			return null;
		}
		
		return new Paper(title, User.getCurrentUser(), conference, paperAbstract, categories, keywords, docText);
	}
}
